﻿using UnityEngine;
using System.Collections;
using System.IO;
using ILRuntime.Runtime.Enviorment;

namespace LCL
{

    public class HelloWorld : MonoBehaviour
    {
        public string ValueTest;
        void Start()
        {
            StartCoroutine(LoadHotFixAssembly());
        }

        IEnumerator LoadHotFixAssembly()
        {
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //这个DLL文件是直接编译HotFix_Project.sln生成的，已经在项目中设置好输出目录为StreamingAssets，在VS里直接编译即可生成到对应目录，无需手动拷贝
#if UNITY_ANDROID
        WWW www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.dll");
#else
            WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/EA4A55FD.dll.bytes");
#endif
            while (!www.isDone)
                yield return null;
            if (!string.IsNullOrEmpty(www.error))
                UnityEngine.Debug.LogError(www.error);
            byte[] dll = www.bytes;
            www.Dispose();

            //PDB文件是调试数据库，如需要在日志中显示报错的行号，则必须提供PDB文件，不过由于会额外耗用内存，正式发布时请将PDB去掉，下面LoadAssembly的时候pdb传null即可
#if UNITY_ANDROID
            www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.pdb");
#else
            www = new WWW("file:///" + Application.streamingAssetsPath + "/EA4A55FD.dll.pdb.bytes");
#endif
            while (!www.isDone)
                yield return null;
            if (!string.IsNullOrEmpty(www.error))
                UnityEngine.Debug.LogError(www.error);
            byte[] pdb = www.bytes;

            new HotfixModule();
            HotfixModule.TestSetDllPdbBytes(dll, pdb);
            HotfixModule.Start();
            HotfixModule.TestSetDllPdbBytes(null, null);
            yield break;
        }
    }


}